import { getClassificationInfo } from "../service/lol.service.js";
// 验证分类唯一
export async function verifyClassificationUniqueness(ctx, next) {
  const { name } = ctx.request.body;
  const classification = await getClassificationInfo({ name });

  if (classification) {
    ctx.app.emit(
      "error",
      {
        code: 10001,
        message: "分类已存在",
        result: "",
      },
      ctx
    );
    return;
  }
  await next();
}
// 修改的时候验证是否有id对应数据
export async function verifyClassificationUpdate(ctx, next) {
  const { id, name } = ctx.request.body;
  if (!id || !name) {
    ctx.app.emit(
      "error",
      {
        code: 10001,
        message: "id 或 name 不能为空",
        result: "",
      },
      ctx
    );
    return;
  }
  // 查找是否有数据
  const classification = await getClassificationInfo({ id });

  if (!classification) {
    ctx.app.emit(
      "error",
      {
        code: 10002,
        message: "分类不存在",
        result: "",
      },
      ctx
    );
    return;
  }
  await next();
}
